package controller

import (
	"fmt"
	"gin/common"
	"gin/model"
	"gin/response"
	"gin/util"
	"github.com/gin-gonic/gin"
	"strings"
	"time"
)

func GoodsList(ctx *gin.Context) {
	var goodsList []model.Goods
	db := common.GetDB()
	shopId := ctx.Query("shopId")
	if len(shopId) == 0 {
		response.Fail(ctx, gin.H{}, "请选择店铺")
		return
	}
	db.Table("cat_meal_goods").Where("shopId=?", shopId).Select("id,name,categoryId,minPrice,pic,shopId").Find(&goodsList)
	var cateList, cateListShow []model.GoodsCate
	db.Table("cat_meal_category").Where("shopId=?", shopId).Select("id,name").Find(&cateList)
	for _, v := range cateList {
		for _, n := range goodsList {
			if v.Id == n.CategoryId {
				v.List = append(v.List, model.Goods{
					Id:         n.Id,
					Name:       n.Name,
					CategoryId: n.CategoryId,
					MinPrice:   n.MinPrice,
					Pic:        n.Pic,
					ShopId:     n.ShopId,
				})
			}
		}
		cateListShow = append(cateListShow, v)
	}
	response.Success(ctx, gin.H{"data": cateListShow}, "success")
}
func Upload(c *gin.Context) {
	c.FullPath()
	f, err := c.FormFile("f1")
	//错误处理
	if err != nil {
		response.Error(c, 500, gin.H{}, "q")
		return
	} else {
		//将文件保存至本项目根目录中
		path := "./upload/data/"
		err2 := c.SaveUploadedFile(f, path+f.Filename)
		fmt.Println("2", err2)
		if err2 != nil {
			return
		}
		response.Success(c, gin.H{"path": util.Substr(path+f.Filename, 1, 0)}, "success")
	}
}
func GoodsDetail(ctx *gin.Context) {
	db := common.GetDB()
	goodsId := ctx.Query("goodsId")
	if len(goodsId) == 0 {
		response.Fail(ctx, gin.H{}, "缺少商品标识")
		return
	}
	//商品详情
	var goodsInfo model.GoodsInfo
	db.Table("cat_meal_goods").Where("id = ? ", goodsId).First(&goodsInfo)
	//商品关联的分类
	var cate model.GoodsCate
	db.Table("cat_meal_category").Where("id = ? ", goodsInfo.CategoryId).First(&cate)
	//商品关联的图片
	var pics []model.Pics
	db.Table("cat_meal_goods_pics").Where("goodsId = ? ", goodsInfo.Id).Find(&pics)
	//商品的规格属性
	var properties []*model.Properties
	PropertyIds := strings.Split(goodsInfo.PropertyIds, ",")
	db.Debug().Table("cat_meal_goods_properties").Where("id in (?)", PropertyIds).Find(&properties)
	var abs []model.Option
	for _, value := range properties {
		fmt.Println(value.Id)
		db.Table("cat_meal_goods_option").Where("`propertyId` = (?)", value.Id).Find(&abs)
		value.ChildsCurGoods = abs
	}
	response.Success(ctx, gin.H{
		"basicInfo":  goodsInfo,
		"category":   cate,
		"properties": properties,
		"content":    goodsInfo.Content,
		"pics":       pics}, "success")
}

func CalculatePrice(ctx *gin.Context) {
	db := common.GetDB()
	goodsId := ctx.Query("goodsId")
	if len(goodsId) == 0 {
		response.Fail(ctx, gin.H{}, "缺少商品标识")
		return
	}
	propertyChildIds := ctx.Query("propertyChildIds")
	if len(propertyChildIds) == 0 {
		response.Fail(ctx, gin.H{}, "缺少规格标识")
		return
	}
	//商品详情
	var price model.SkuPrice
	db.Debug().Table("cat_meal_goods_sku").Where("goodsId = ? and propertyChildIds = ? ", goodsId, propertyChildIds).First(&price)
	response.Success(ctx, gin.H{"info": price}, "success")
}

func TestLogin(c *gin.Context) {
	var username = c.PostForm("username")
	var password = c.PostForm("password")
	if username != "csapp001" {
		response.Fail(c, gin.H{}, "用户名错误")
	}
	if password != "123456" {
		response.Fail(c, gin.H{}, "密码错误")
	}

	response.Success(c, gin.H{
		"username": username,
		"password": password,
		"age":      18,
		"sex":      "男",
		"token":    util.MD5(time.Now().String()),
	}, "ok")
}
